# COPYRIGHT BLABLA

# README:
# Turn left/right (A,D) each press will change speed

from ogrebase import OgreView, EventListener
import ogre.io.OIS as OIS
import ogre.renderer.OGRE as ogre
import ogre.gui.QuickGUI as QuickGui
import time
import math

class QuickGuiBase(OgreView, EventListener):
    def __init__(self, title="QuickGui...", fullscreen=False, \
            resolution=(1024, 768), resDir='media', \
            renderSystem=''):
        OgreView.__init__(self, title, fullscreen, resolution, resDir, renderSystem)
        EventListener.__init__(self,self.RenderWindow, True, True)
        self.set_window_resolution(resolution[0], resolution[1])
        self._init_quick_gui()

    def __del__(self):
        EventListener.__del__(self)
#        OgreView.__del__(self)


    def _init_render_window(self):
        QuickGui.registerScriptReader()
        OgreView._init_render_window(self)

    def _init_quick_gui(self):
        self.GuiRoot = QuickGui.Root()

        QuickGui.SkinTypeManager.getSingleton().loadTypes()
        GuiManagerDesc = QuickGui.GUIManagerDesc()
        GuiManagerDesc.sceneManager = self.SceneManager
        GuiManagerDesc.viewport = self.Camera.getViewport()
        GuiManagerDesc.queueId = ogre.RENDER_QUEUE_OVERLAY
        self.GuiManager = QuickGui.Root.getSingleton().\
                createGUIManager(GuiManagerDesc)

    def mouseMoved(self, evt):
        EventListener.mouseMoved(self, evt)
        self.GuiManager.injectMousePosition(evt.get_state().X.abs, evt.get_state().Y.abs)
        if evt.get_state().Z.rel != 0:
            self.GuiManager.injectMouseWheelChange(evt.state.Z.rel)

        return True

    def mousePressed(self, evt, id):
        EventListener.mousePressed(self, evt, id)
        self.GuiManager.injectMouseButtonDown(QuickGui.MouseButtonID.values[id])
        return True

    def mouseReleased(self, evt, id):
        EventListener.mouseReleased(self, evt, id)
        self.GuiManager.injectMouseButtonUp(QuickGui.MouseButtonID.values[id])
        return True

    def keyPressed(self, evt):
        EventListener.keyPressed(self, evt)
        self.GuiManager.injectChar(evt.text)
        self.GuiManager.injectKeyDown(QuickGui.KeyCode.values[evt.key])

        return True

    def keyReleased(self, evt):
        EventListener.keyReleased(self, evt)
        self.GuiManager.injectKeyUp(QuickGui.KeyCode.values[evt.key])

        return True
        



class QuickGuiView(object):
    def __init__(self):
        self.OgreView = QuickGuiBase()
        self.QuitApplication = False

        self.OgreView.register_key_event(OIS.KC_ESCAPE, self._quit)
        self.OgreView.register_key_event(OIS.KC_A, self._left)
        self.OgreView.register_key_event(OIS.KC_D, self._right)
        self.Angle = 0
        self.Turn = 0.01

    def __del__(self):
        if self.OgreView is not None:
            self.deinit()

    def deinit(self):
        del self.OgreView
        self.OgreView = None

    def run(self):
        self._init_scene()
        self._init_base_gui()

        while not self.QuitApplication:
            self.OgreView.render()
            self.OgreView.update()
            self.set_cam_pos(self.Angle)
            time.sleep(0.01)
            self.Angle += self.Turn

    def _init_scene(self):
        self.OgreView.SceneManager.setAmbientLight(\
                ogre.ColourValue(0.7, 0.7, 0.7))
        self.OgreView.SceneManager.setSkyDome(True, 'Examples/CloudySky', 4,8)
        self.Light = self.OgreView.SceneManager.createLight('mainlight')
        self.Light.setPosition(ogre.Vector3(20, 80, 50))

        self.RootNode = self.OgreView.SceneManager.getRootSceneNode()

        self.Ogre = self.OgreView.SceneManager.createEntity('Ogre', \
                'ogrehead.mesh')
        self.NodeOgre = self.RootNode.createChildSceneNode('nodeOgre')
        self.NodeOgre.setPosition(ogre.Vector3(0,0,0))
        self.NodeOgre.attachObject(self.Ogre)

        self.set_cam_pos(self.Angle)

    def _init_base_gui(self):
        sheet = self.OgreView.GuiManager.getActiveSheet()
        buttonDesc = \
                QuickGui.WidgetDescFactoryManager.getSingleton().createWidgetDesc('ButtonDesc')
        windowDesc = \
                QuickGui.WidgetDescFactoryManager.getSingleton().createWidgetDesc('WindowDesc')

        buttonDesc.widget_name = "MyButton"
        buttonDesc.dimensions.size = QuickGui.Size(100,25)
        buttonDesc.dimensions.position = QuickGui.Point(50,50)
        button = sheet.createButton(buttonDesc)
        buttonDesc.widget_name = "MyButton2"
        buttonDesc.dimensions.size = QuickGui.Size(110,25)
        buttonDesc.dimensions.position = QuickGui.Point(100,100)
        button2 = sheet.createButton(buttonDesc)
        windowDesc.widget_name = "TestWindow"
        windowDesc.dimensions.size = QuickGui.Size(300, 300)
        windowDesc.dimensions.position = QuickGui.Point(150,150)
        win1 = sheet.createWindow(windowDesc)
        buttonDesc.widget_name = "MyButton3"
        buttonDesc.dimensions.size = QuickGui.Size(110,25)
        buttonDesc.dimensions.position = QuickGui.Point(100,100)
        button3 = win1.createButton(buttonDesc)

        button.setText('Test')

    def set_cam_pos(self, angle):
        self.OgreView.Camera.setPosition(ogre.Vector3(math.cos(angle)*200,\
                -10, math.sin(angle)*200))
        self.OgreView.Camera.lookAt(ogre.Vector3(0, 0, 1))
       

    def _quit(self, pressed):
        if not pressed:
            self.QuitApplication = True

    def _left(self, pressed):
        if pressed:
            self.Turn += 0.01

    def _right(self, pressed):
        if pressed:
            self.Turn -= 0.01

if __name__ == '__main__':
    try:
        t = QuickGuiView()
        t.run()
        t.deinit()
    except ogre.OgreException, e:
        print e

